#include "album.h"
#include "ui_album.h"
#include "eventcenter.h"
#include "UIEditor/uilib.h"
#include "GameData/jsontranslator.h"
#include "ResourceManger/filemanager.h"
#include "GameUI/recorditem.h"
#include <QDebug>
#include <QDir>
Album::Album(QWidget *parent) :QWidget(parent),
    ui(new Ui::Album)
{
    Init();
}

Album::~Album()
{
    delete ui;
}

void Album::AddImage(QString path)
{
    QDir dir;
    path = dir.relativeFilePath(path);
    if(img.contains(path))
        return;
    img.push_back(path);
    if(img.count() == 1)
        label->setStyleSheet("border-image:url(" + path + ");");
}

void Album::RemoveImage(QString path)
{
    if(!img.contains(path))
        return;
    int i = 0;
    while(i < img.count())
    {
        if(img[i] == path)
        {
            img.remove(i);
            break;
        }
        else
        {
            i++;
        }
    }
    if(img.count() > 0 )
    {
        curImageIndex = 0;
        label->setStyleSheet("border-image:url(" + img[curImageIndex] + ");");
    }
    else
    {
        curImageIndex = 0;
        label->setStyleSheet("");
    }
}

void Album::ClearImage()
{
    curImageIndex = 0;
    label->setStyleSheet("");
    img.clear();
}

void Album::SetImage(QVector<QString> vec)
{
    ClearImage();
    for(auto path:vec)
    {
        AddImage(path);
    }
}

void Album::showEvent(QShowEvent *event)
{
    QWidget::showEvent(event);
    return;
    curImageIndex = 0;
    if(img.count() > 0)
        label->setStyleSheet("border-image:url(" + img[curImageIndex] + ");");
}

void Album::Init()
{
    ui->setupUi(this);
    ui->BackBtn->SetSelectedDisabled(true);
    ui->lastBtn->SetSelectedDisabled(true);
    ui->nextBtn->SetSelectedDisabled(true);
    ui->homeBtn->SetSelectedDisabled(true);

    QString qss0 = "QWidget#album{border-image:url(\"";
    QString qss1 = "\");padding-left: 20px;padding-right: 20px;padding-top:30px;padding-bottom:30px;}";
    if(UILib::Instance().labels["图鉴-容器"])
    {
        ui->album->setStyleSheet(qss0 + UILib::Instance().labels["图鉴-容器"]->img + qss1);
    }
    connect(&EventCenter::Instance(),&EventCenter::UIChange,this,[=](QString name){
        if(name == "图鉴-容器" && UILib::Instance().labels[name])
        {
            ui->album->setStyleSheet(qss0 + UILib::Instance().labels[name]->img + qss1);
        }
    });

    QMap<QString,MyButton*> btns =
    {
        {"返回游戏",ui->BackBtn},
        {"返回主页",ui->homeBtn},
        {"图鉴-上一张",ui->lastBtn},
        {"图鉴-下一张",ui->nextBtn}
    };

    for(auto i = btns.begin();i != btns.end();i++)
    {
        if(UILib::Instance().btns.contains(i.key()) && UILib::Instance().btns[i.key()])
        {
            i.value()->info = UILib::Instance().btns[i.key()];
            i.value()->Refresh(i.value()->info);
        }
    }

    label = new QLabel();
    ui->album->setLayout(new QHBoxLayout());
    ui->album->layout()->addWidget(label);
    ui->album->layout()->setContentsMargins(50,50,50,50);

    connect(ui->lastBtn,&QPushButton::clicked,this,[=](){
        qDebug()<<__FILE__<<__LINE__<<"上一张图片";
        ChangeImage(-1);
    });

    connect(ui->nextBtn,&QPushButton::clicked,this,[=](){
        qDebug()<<__FILE__<<__LINE__<<"下一张图片";
        ChangeImage(1);
    });
    //数据持久化
    connect(&EventCenter::Instance(),&EventCenter::Save,this,[=](){Save();});

    //添加图鉴
    connect(&EventCenter::Instance(),&EventCenter::AddAlbum,this,[=](QString path){
        AddImage(path);
    });
    //删除图鉴
    connect(&EventCenter::Instance(),&EventCenter::DeleteAlbum,this,[=](QString path){
        RemoveImage(path);
    });
    //阅读存档图鉴
    connect(&EventCenter::Instance(),&EventCenter::RunRecord,this,[=](RecordItem* item){
        if(item)
            SetImage(item->album);
    });
    connect(&EventCenter::Instance(),&EventCenter::RefreshAlbum,this,[=](QVector<QString> album){
        SetImage(album);
    });
}

void Album::ChangeImage(int offset)
{
    int tmp = curImageIndex + offset;
    if((offset != 1 && offset != -1) || tmp >= img.count() || tmp < 0)
        return;
    curImageIndex = tmp;
    label->setStyleSheet("border-image:url(" + img[curImageIndex] + ");");
}

void Album::Save()
{
    JsonTranslator jt;
    QJsonObject* ob = jt.ToJson(img);
    FileManager::Instance().Write(FileManager::Instance().albumPath,jt.Json2Byte(ob));
    if(ob)
    {
        delete ob;
        ob = nullptr;
    }
}
